$(function()
{

	if ('close' != $('#period_status').val())
	{
		recalc();
	}	
		
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------	

	$('#archive').change(function(){
		
		var aDate = $(this).val();
		window.location.href = 'http://time.vlfn.nl/?archive=' + aDate;
//		alert(aDate);
	});
	
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------	

	function send_time(id, send_hour)
	{
//		alert (id + ' ' + send_hour);
		var month = $('#month').val();
		var year  = $('#year').val();	
		$.ajax(
		{
			type: 'POST',
			url: '/index/update',
			data: 'id=' + encodeURIComponent( id ) + '&send_hour=' + encodeURIComponent( send_hour ) + '&year=' + encodeURIComponent( year )
			 	+ '&month=' + encodeURIComponent( month ),
			dataType: 'json',
			success: function(r)
			{
			}
		});
	}
	
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	$('.hour').change(function(){
		var id  = $(this).attr('id');
		var send_hour = $(this).val() * 1;
		
		recalc();
	
		send_time(id, send_hour);
	
	});
	
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	function recalc()
	{
		var all_total = 0;
		var all_overtime = 0;
		var d_hour = 0;
		var sum_total = 0;
		var sum_free = 0;
		
		$(".total").each(function()
		{
			var day = $(this).attr('day');
			var d_hour = 0;
			var f_hour = 0;
			
			$("input[day='" + day + "']").each(function(){
				if( $(this).hasClass('billable') ) 
				{
					var cur_hour = $(this).val() * 1 ;
					d_hour += cur_hour;
				
				}
				else {
					var cur_hour = $(this).val() * 1 ;
					f_hour += cur_hour;
				}
			});
				
				if( 8 < d_hour )
				{
					all_total += 8;
					all_overtime += (d_hour - 8);
					
					$('#total_' + day).html('&nbsp;' + sprintf("%01.1f", 8 ) + ' &nbsp;');
					$('#overtime_' + day).html('&nbsp; ' + sprintf("%01.1f", (d_hour - 8)) + ' &nbsp;');
				}
				else
				{
					all_total += d_hour;
					
					$('#total_' + day).html('&nbsp;' + sprintf("%01.1f", d_hour ) + '&nbsp;');
					$('#overtime_' + day).html('&nbsp;  &nbsp;');
				}
				$('#sum_total_' + day).html('&nbsp;' + sprintf("%01.1f", d_hour ) + '&nbsp;');
				$('#sum_free_' + day).html('&nbsp;' + sprintf("%01.1f", f_hour ) + '&nbsp;');
				
			sum_total += d_hour;
			sum_free += f_hour;
		});
		
		$(".sum_total").each(function()
		{
			var sum_hour = 0;
			var project = $(this).attr('project');
			$("input[project='" + project + "']").each(function(){
				var cur_hour = $(this).val() * 1 ;
				sum_hour += cur_hour;
			});
			
			$('#tr_' + project).html('&nbsp;' + sprintf("%01.1f", sum_hour ) + '&nbsp;');
		});	
		
		$('#all_total').html('&nbsp;' + sprintf("%01.1f", all_total) + '&nbsp;');
		$('#all_overtime').html('&nbsp;' + sprintf("%01.1f", all_overtime) + '&nbsp;');
		$('#sum_total').html('&nbsp;' + sprintf("%01.1f", sum_total) + '&nbsp;');
		$('#sum_free').html('&nbsp;' + sprintf("%01.1f", sum_free) + '&nbsp;');
	}
	
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------	
	
});

